function sampleArray(arr: any) {
    const len = arr.length;
    if (len <= 10000) {
        return arr; // 长度不足10000时直接返回原数组
    }

    const result = [];
    const step = len / 10000; // 计算采样步长
    for (let i = 0; i < 10000; i++) {
        // 计算当前采样点的索引（使用四舍五入确保均匀分布）
        const index = Math.round(i * step);
        result.push(arr[index]);
    }
    return result;
}

// 断面分析
export const duanmianOption = (data: any) => {
    const ps = sampleArray(data);   
    const seriesData = ps.map((item: any) => item[2].toFixed(2));
    const xAxisData = ps.map((item: any) => item[3].toFixed(2));

    // 计算最小值和最大值
    const minValue = seriesData.reduce((acc: any, cur: any) => cur < acc ? cur : acc);
    const maxValue = seriesData.reduce((acc: any, cur: any) => cur > acc ? cur : acc);
    const range = (minValue - (maxValue - minValue) * 0.1).toFixed(2);

    const dataZoomConfig = [ // 数据缩放组件
        {
            type: 'inside', // 内置型，支持鼠标滚轮缩放
            start: 0, // 初始缩放范围
            end: 100,// 显示前20%的数据
            height: 20,
            bottom: -12,
        },
        {
            start: 0, // 滑动条型
            end: 20,
            height: 20,
            bottom: -12,

        }
    ]
    let option = {
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: xAxisData
        },
        yAxis: {
            type: 'value',
            min: range,  // 动态设置最小值
        },
        dataZoom: dataZoomConfig,
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                lineStyle: {
                    color: '#57617B'
                }
            }
        },
        grid: {
            left: '1%',
            top: '5%',
            right: '0%',
            bottom: "5%", // 有缩放控件时底部留更多空间
            containLabel: true
        },
        series: [
            {
                data: seriesData,
                type: 'line'
            }
        ]
    };
    return option
}
// 方量分析
// export const fanggliangOption = (data: any) => {
//     const ps = sampleArray(data);   

//     const seriesData = ps.map((item: any) => [item[3].toFixed(2), item[4].toFixed(2), item[2].toFixed(2)]);
//     // 计算 z 值的范围
//     const zValues = seriesData.map((item: any) => item[2]);

//     // 计算最小值和最大值
//     const minZ = zValues.reduce((acc: any, cur: any) => cur < acc ? cur : acc);
//     const maxZ = zValues.reduce((acc: any, cur: any) => cur > acc ? cur : acc);

//     let option = {
//         visualMap: {
//             show: false,
//             dimension: 2,  // 使用第三个维度（z值）控制颜色
//             min: minZ,
//             max: maxZ,
//             orient: 'vertical',
//             right: 10,
//             top: 'center',
//             calculable: true,
//             inRange: {
//                 color: ['#05aff1ff', '#f52a0fff']
//             }
//         },
//         tooltip: {
//             trigger: 'item',
//             axisPointer: {
//                 type: 'cross'
//             },
//             formatter: (params: any) => {
//                 const height = seriesData.find((item: any) => ((item[0] == params.value[0]) && (item[1] == params.value[1])))
//                 var relVal = ""
//                 relVal += params.marker + ' ' + '<span style="text-align:left; display:inline-block; width:60px;" >横向距离</span>'
//                 relVal += '<span style="text-align:right; display:inline-block; margin-left:20px;  font-size: 12px; font-weight: bold; color: #403f3fff">' + params.value[0] + '</span>'
//                 relVal += '<br/>' + params.marker + ' ' + '<span style="text-align:left; display:inline-block; width:60px;" >纵向距离</span>'
//                 relVal += '<span style="text-align:right; display:inline-block;  margin-left:20px; font-size: 12px; font-weight: bold; color: #403f3fff">' + params.value[1] + '</span>'
//                 relVal += '<br/>' + params.marker + ' ' + '<span style="text-align:left; display:inline-block; width:60px;" >高度</span>'
//                 relVal += '<span style="text-align:right; display:inline-block; margin-left:20px;  font-size: 12px; font-weight: bold; color: #403f3fff">' + height[2] + '</span>'
//                 return relVal
//             },
//         },
//         xAxis: [
//             {
//                 type: 'value',
//             }
//         ],
//         yAxis: [
//             {
//                 type: 'value'
//             }
//         ],
//         grid: {
//             left: '4%',
//             top: '5%',
//             right: '4%',
//             bottom: '5%',
//             containLabel: true
//         },
//         series: [
//             {
//                 type: 'scatter',
//                 symbolSize: 5,
//                 data: seriesData,
//             }
//         ]
//     }
//     return option
// }

export const fanggliangOption = (data: any) => {
    const ps = sampleArray(data);   
    const seriesData = ps.map((item: any) => [item[3].toFixed(2), item[4].toFixed(2), item[2].toFixed(2)]);
    
    // 计算 x/y/z 轴的范围
    const xValues = seriesData.map((item: any) => parseFloat(item[0]));
    const yValues = seriesData.map((item: any) => parseFloat(item[1]));
    const zValues = seriesData.map((item: any) => parseFloat(item[2]));
    
    const minX = Math.min(...xValues);
    const maxX = Math.max(...xValues);
    const minY = Math.min(...yValues);
    const maxY = Math.max(...yValues);
    const minZ = Math.min(...zValues);
    const maxZ = Math.max(...zValues);
    
    // 关键修改：计算统一的刻度范围
    const commonMin = Math.min(minX, minY);
    const commonMax = Math.max(maxX, maxY);
    const commonRange = commonMax - commonMin;
    
    // 计算刻度间隔（这里设置为5个主要刻度）
    const interval = Math.ceil(commonRange / 5);
    
    let option = {
        visualMap: {
            show: false,
            dimension: 2,
            min: minZ,
            max: maxZ,
            orient: 'vertical',
            right: 10,
            top: 'center',
            calculable: true,
            inRange: {
                color: ['#05aff1ff', '#f52a0fff']
            }
        },
        tooltip: {
            trigger: 'item',
            axisPointer: {
                type: 'cross'
            },
            formatter: (params: any) => {
                var relVal = ""
                relVal += params.marker + ' ' + '<span style="text-align:left; display:inline-block; width:60px;" >横向距离</span>'
                relVal += '<span style="text-align:right; display:inline-block; margin-left:20px;  font-size: 12px; font-weight: bold; color: #403f3fff">' + params.value[0] + '</span>'
                relVal += '<br/>' + params.marker + ' ' + '<span style="text-align:left; display:inline-block; width:60px;" >纵向距离</span>'
                relVal += '<span style="text-align:right; display:inline-block;  margin-left:20px; font-size: 12px; font-weight: bold; color: #403f3fff">' + params.value[1] + '</span>'
                relVal += '<br/>' + params.marker + ' ' + '<span style="text-align:left; display:inline-block; width:60px;" >高度</span>'
                relVal += '<span style="text-align:right; display:inline-block; margin-left:20px;  font-size: 12px; font-weight: bold; color: #403f3fff">' + params.value[2] + '</span>'
                return relVal
            },
        },
        // 关键修改：设置相同的坐标轴刻度范围
        xAxis: {
            type: 'value',
            min: commonMin,
            max: commonMax,
            interval: interval,
            axisLabel: { formatter: '{value}' }
        },
        yAxis: {
            type: 'value',
            min: commonMin,
            max: commonMax,
            interval: interval,
            axisLabel: { formatter: '{value}' }
        },
        grid: {
            left: '4%',
            top: '5%',
            right: '4%',
            bottom: '5%',
            containLabel: true
        },
        series: [{
            type: 'scatter',
            symbolSize: 5,
            data: seriesData,
        }]
    }
    return option;
}